package com.peng.auctionsystem;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import Widget.CustomToolBar;
import Widget.NoScrollViewPager;
import base.BaseActivity;
import base.BaseFragment;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ui.activity.PublishActivity;
import ui.adapter.HomepageFragmentAdapter;
import ui.fragment.HomepageFragment;
import ui.fragment.MyFragment;

/**
 * 主页
 */
public class MainActivity extends BaseActivity {


    @Bind(R.id.custom_toolbar)
    CustomToolBar customToolbar;
    @Bind(R.id.main_tv_homepage)
    TextView mainTvHomepage;
    @Bind(R.id.main_tv_publish)
    TextView mainTvPublish;
    @Bind(R.id.main_tv_my)
    TextView mainTvMy;
    @Bind(R.id.main_ll_bottom)
    LinearLayout mainLlBottom;
    @Bind(R.id.main_nsvp_pager)
    NoScrollViewPager mainNsvpPager;

    private TextView  mTvTitle;
    private BaseFragment  mCurrentFragment;
    private HomepageFragment mHomepageFragment;
    private MyFragment  mMyFragment;
    private HomepageFragmentAdapter mAdapter;
    private List<BaseFragment>  fragmentList;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        customToolbar.findTextView(R.id.left_tv).setVisibility(View.GONE);
        customToolbar.findImageView(R.id.left_iv).setVisibility(View.GONE);
        customToolbar.findTextView(R.id.right_tv_main).setVisibility(View.GONE);
        customToolbar.findTextView(R.id.right_tv_sub).setVisibility(View.GONE);
        mTvTitle = customToolbar.findTextView(R.id.center_tv);
        mainNsvpPager.setOnPageChangeListener(new MyPagerChangeListener());
        mTvTitle.setText("首页");
        fragmentList = new ArrayList<>();
        mHomepageFragment = new HomepageFragment();
        mMyFragment = new MyFragment();
        fragmentList.add(mHomepageFragment);
        fragmentList.add(mMyFragment);
        mAdapter = new HomepageFragmentAdapter(getSupportFragmentManager(),fragmentList);

        changeBottom(1);
        mainNsvpPager.setAdapter(mAdapter);
        mainNsvpPager.setCurrentItem(0);  //初始化显示第一个页面
    }

    /**
     *
     * @param flag
     */
    private void changeBottom(int flag) {
        mainTvHomepage.setTextColor(flag == 1? Color.parseColor("#33CCFF") : Color.parseColor("#000000"));
        mainTvMy.setTextColor(flag == 2? Color.parseColor("#33CCFF") : Color.parseColor("#000000"));
    }

    @OnClick({R.id.main_tv_homepage, R.id.main_tv_publish, R.id.main_tv_my})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.main_tv_homepage:
                if (mHomepageFragment == null) {
                    mHomepageFragment = new HomepageFragment();
                }
                if (mCurrentFragment instanceof HomepageFragment){
                    return;
                }
                mCurrentFragment = mHomepageFragment;
                mTvTitle.setText("首页");
                changeBottom(1);
                mainNsvpPager.setCurrentItem(0);
                break;
            case R.id.main_tv_publish:
                Intent intent = new Intent(this,PublishActivity.class);
                intent.putExtra("isPublish", 1);
                intent.putExtra("id",0);
                startActivity(intent);
                break;
            case R.id.main_tv_my:
                if (mMyFragment == null) {
                    mMyFragment = new MyFragment();
                }
                if (mCurrentFragment instanceof MyFragment){
                    return;
                }
                mCurrentFragment = mMyFragment;
                mTvTitle.setText("我的");
                changeBottom(2);
                mainNsvpPager.setCurrentItem(1);
                break;
        }
    }


    public class MyPagerChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int arg0) {
            switch (arg0) {
                case 0:
                    break;
                case 1:
                    break;
            }
        }
    }
}
